package com.ftg.product.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.diboot.core.vo.*;
import com.ftg.product.exam.entity.SysOrganizations;
import com.ftg.product.exam.dto.SysOrganizationsDTO;
import com.ftg.product.exam.vo.SysOrganizationsListVO;
import com.ftg.product.exam.vo.SysOrganizationsDetailVO;
import com.ftg.product.exam.service.SysOrganizationsService;

import lombok.extern.slf4j.Slf4j;
import javax.validation.Valid;
import java.util.List;

/**
* 组织机构表,
1.1 组织机构信息（学院名称、学院地址、学院说明、）
1.2 部门信息(名称、说明、地址) 相关Controller
* @author kangjx
* @version 1.0
* @date 2021-01-14
* Copyright © ftg
*/
@Api(tags = "组织机构表")
@RestController
@RequestMapping("/sysOrganizations")
@Slf4j
public class SysOrganizationsController extends BaseCustomCrudRestController<SysOrganizations> {
    @Autowired
    private SysOrganizationsService sysOrganizationsService;

    /***
    * 查询ViewObject的分页数据
    * <p>
    * url请求参数示例: /list?field=abc&pageIndex=1&orderBy=abc:DESC
    * </p>
    * @return
    * @throws Exception
    */
    @ApiOperation(value = "获取列表分页数据")
    @GetMapping("/list")
    public JsonResult getViewObjectListMapping(SysOrganizationsDTO queryDto, Pagination pagination) throws Exception{
        return super.getViewObjectList(queryDto, pagination, SysOrganizationsListVO.class);
    }

    @ApiOperation(value = "通过名称，判断是否存在")
    @GetMapping("/checkName/{orgname}")
    public JsonResult getViewObjectForName(@PathVariable String orgname) throws Exception{
        QueryWrapper<SysOrganizations> qw = new QueryWrapper<>();
        qw.eq("orgname",orgname);
        return super.getEntityList(qw);
//        return super.getViewObjectList(orgname, SysOrganizationsListVO.class);
    }

    /***
    * 根据资源id查询ViewObject
    * @param id ID
    * @return
    * @throws Exception
    */
    @ApiOperation(value = "根据ID获取详情数据")
    @GetMapping("/{id}")
    public JsonResult getViewObjectMapping(@PathVariable("id")Long id) throws Exception{
        return super.getViewObject(id, SysOrganizationsDetailVO.class);
    }

    /***
    * 创建资源对象
    * @param entity
    * @return JsonResult
    * @throws Exception
    */
    @ApiOperation(value = "新建数据")
    @PostMapping("/")
    public JsonResult createEntityMapping(@Valid @RequestBody SysOrganizations entity) throws Exception {
        return super.createEntity(entity);
    }

    /***
    * 根据ID更新资源对象
    * @param entity
    * @return JsonResult
    * @throws Exception
    */
    @ApiOperation(value = "根据ID更新数据")
    @PutMapping("/{id}")
    public JsonResult updateEntityMapping(@PathVariable("id")Long id, @Valid @RequestBody SysOrganizations entity) throws Exception {
        return super.updateEntity(id, entity);
    }

    /**
     * 根据ID删除部门，同时删除部门内的员工
     * @param id 部门编号
     * @return
     */
    @ApiOperation(value = "根据ID删除数据")
    @DeleteMapping("/delete/{id}")
    public int delete(@ApiParam("部门编号") @PathVariable Long id){
        return sysOrganizationsService.delete(id);
    }

    @ApiOperation(value = "根据多条ID删除多条数据")
    @DeleteMapping("/deletes/{ids}")
    public int deletes(@ApiParam("多条ID") @PathVariable List<Long> ids){
        return sysOrganizationsService.deletes(ids);
    }

} 